package edu.jvn.ict.locationsharing.informationexchange;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import edu.jvn.ict.locationsharing.MemberInfo;
import edu.jvn.ict.locationsharing.MemberServer;
import edu.jvn.ict.locationsharing.Utility;

public class JoiningGroupMessage extends ExchangeMessage {
	/**
	 * 
	 */
	public static final int MESSAGE_ID = 4;
	private static final long serialVersionUID = -4037443223148917871L;
	private int ip;
	private int port;
	private long joiningTimestamp;
	private long phoneNumber;

	public int getIp() {
		return ip;
	}

	public void setIp(int ip) {
		this.ip = ip;
	}

	public long getJoiningTimestamp() {
		return joiningTimestamp;
	}

	public void setJoiningTimestamp(long joiningTimestamp) {
		this.joiningTimestamp = joiningTimestamp;
	}

	@Override
	public void process(ObjectInputStream in, ObjectOutputStream out)
			throws IOException {
		MemberServer.exchangeInformation(this, 1, false);

		MemberInfo memberInfo = MemberServer.createMemberFromJGM(this);
		MemberServer.addToDictionary(memberInfo);

	}

	public long getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(long phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String toString() {
		return Utility.APP_START_MESSAGE + Integer.toString(MESSAGE_ID) + ","
				+ Integer.toString(ip) + "," + Long.toString(joiningTimestamp) + "," + Integer.toString(port);
	}
	
	public JoiningGroupMessage(String message){
		String[] split = message.split(",");
		ip = Utility.ipToInt(split[0]);
		joiningTimestamp = Long.parseLong(split[1]);
		port = Integer.parseInt(split[2]);
	}
	
	public JoiningGroupMessage(){
		
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}
}
